/* 
  Copyright (c) SilverLining 2015
  Date:    2016-1-11
  File:    7za.h  
  Description: 
	Header for the 7zip command line wrapper.
*/

#ifndef __7ZIP_H__
#define __7ZIP_H__

namespace m
{
	class package;

	namespace compression
	{
		/// <summary>
		/// Wraps the command-line functionality we need from 7za.
		/// </summary>
		class _7za
		{
		public:
			_7za();
			~_7za();

			/// <summary>
			/// Returns if the specified package exists.
			/// </summary> 
			/// <param name="packageName">The name of the package to create.</param>
			/// <returns>True if the package exists.</returns>
			bool package_exists(const wchar_t* packageName);

			/// <summary>
			/// Creates the specified package out of the given package object.
			/// </summary>
			/// <param name="packageName">The name of the package to create.</param>
			/// <param name="packageFiles">The specified package file location.</param>
			void create_package(const wchar_t* packageName, const wchar_t* packageFiles);

			/// <summary>
			/// Extract the specified file into the buffer.
			/// </summary>
			/// <param name="packageName">The name of the package to create.</param>
			/// <param name="fileName">The name and path of the file to extract.</param>
			/// <param name="buffer">The name of the buffer to extract the file into.</param>
			/// <remarks>If a file is not included in a package, the size of the buffer will be 0.</remarks>
			void extract_file(const wchar_t* packageName, const wchar_t* fileName, m::io::file_buffer* buffer);

		private:
			DWORD CreateChildProcess(io::file_buffer* outBuf, LPCWSTR WorkingDir, LPCWSTR Format, ...);
			static void ReadFromPipe(HANDLE hOut, io::file_buffer* buf);
			static void GetArchiverPath(WCHAR ArchiverPath[MAX_PATH]);
			void Reset();

			WCHAR* _CommandBuffer;
			HANDLE _hChildStd_OUT_Rd;
			HANDLE _hChildStd_OUT_Wr;
			HANDLE _hChildStd_ERR_Rd;
			HANDLE _hChildStd_ERR_Wr;
		};
	} // namespace compression
}

#endif //__7ZIP_H__

